﻿/***************************************************************************
 * 文 件 名:   HKMonoUtility
 * 
 * 描    述:   Mono单例工具类
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭、谭潇杰
 * 创建时间：  2024/5/1
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
****************************************************************************/

using QFramework;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class HKMonoUtility : MonoSingleton<HKMonoUtility>
{
    /// <summary>
    /// 从 StreamingAssets 中获取Json文件并解析，通过callback返回
    /// </summary>
    public void GetJsonFromStreamingAssets<T>(string filePath, Action<T> callback) where T : class
    {
        StartCoroutine(IE_GetJson(filePath, callback, false));
    }

    /// <summary>
    /// 从 URL 中获取Json文件并解析，通过callback返回
    /// </summary>
    public void GetJsonFromURL<T>(string url, Action<T> callback, string header = "", string end = "") where T : class
    {
        StartCoroutine(IE_GetJson(url, callback, true, header, end));
    }

    IEnumerator IE_GetJson<T>(string url, Action<T> callback, bool isURL, string header = "", string end = "") where T : class
    {
        UnityWebRequest request;

        if (isURL)
        {
            request = UnityWebRequest.Get(url);
        }
        else
        {
            url = Application.streamingAssetsPath + "/" + url;
            request = UnityWebRequest.Get(url);
        }

        yield return request.SendWebRequest();

        if (request.error != null)
        {
            Debug.LogError("请求 " + url + " 文件错误！");
        }
        else
        {
            string dataStr = request.downloadHandler.text;

            if (isURL && header != "")
            {
                dataStr = header + dataStr + end;
            }

            Debug.Log("获取的json数据为：" + dataStr);
            T t = JsonUtility.FromJson<T>(dataStr);
            callback?.Invoke(t);
        }
    }

    /// <summary>
    /// 从 StreamingAssets 中加载Sprite，通过callback返回
    /// </summary>
    public void GetSpriteFromStreamingAssets(string texturePath, Action<Sprite> callback)
    {
        StartCoroutine(IE_GetSpriteFromStreamingAssets(texturePath, callback));
    }

    IEnumerator IE_GetSpriteFromStreamingAssets(string texturePath, Action<Sprite> callback)
    {
        if (!texturePath.StartsWith("/"))
        {
            texturePath = "/" + texturePath;
        }

        string path = Application.streamingAssetsPath + texturePath;

        UnityWebRequest request = UnityWebRequestTexture.GetTexture(path);

        yield return request.SendWebRequest();

        if (request.result != UnityWebRequest.Result.Success)
        {
            Debug.LogError("请求 " + texturePath + " 文件错误！");
        }
        else
        {
            Texture2D texture = DownloadHandlerTexture.GetContent(request);
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            callback?.Invoke(sprite);
        }
    }
}
